//
//  NwayTreeCollection.cpp
//  CollectionTest
//
//  Created by Dusan Vujosevic on 2013-11-03.
//  Copyright (c) 2013 Suavetech. All rights reserved.
//

#include "NwayTreeCollection.h"
#include "NWayTree.h"
#include <fcntl.h>
#include <unistd.h>

NwayTreeCollection::~NwayTreeCollection()
{
	delete _nwayTree;
}

bool NwayTreeCollection::init()
{
	_nwayTree = new Tree;
	
	return treeInit(_nwayTree) == 0;
}

void NwayTreeCollection::reset()
{
	delete _nwayTree;
	init();
}

void NwayTreeCollection::loadElements(const void* randBytes, size_t elemCount)
{
	elemType* randElem = (elemType*)randBytes;
	for(size_t u = 0; u < elemCount; u++)
		treeInsert(_nwayTree, randElem[u]);
}

bool NwayTreeCollection::findElement(elemType elem)
{
	return treeFind(_nwayTree, elem) == 1;
}

void NwayTreeCollection::runTest()
{
	static const elemType member = 58116241790959135LL;
	static const elemType search = 3865996416352615967LL;
	
	NwayTreeCollection c(1000);
	c.init();
	c.loadElements(&member, 1);
	c.findElement(search);
	
	
	int urfd = open("/dev/urandom", O_RDONLY);
	while(true)
	{
		elemType t;
		read(urfd, &t, sizeof(t));
		c.findElement(t);
	}
}
